/*******************************************************************************
																				
	DESCRIPTION:  	This do file produces Table 4 and Appendix Table A7.
	
*******************************************************************************/

clear all
global id_code 134
set seed 2110

/*******************************************************************************
*	Load stored results
********************************************************************************/
estimates use "$output/${id_code}_TwoSpell_Analysis.ster"

/*******************************************************************************
*	Fixed vs Variable / Obs vs Non-obs table: Two-spell sample
********************************************************************************/
local x 0

* Print some stats:
di "Number of observations: `e(N)'"
di "Fixed observable heterogeneity is `: di %5.2f `= _b[main_`x'y:v_obs_pers]/_b[main_`x'y:v_pers]*100''% of total fixed heterogeneity."
di "Total observable heterogeneity (two-spell sample) is `: di %5.2f `= (_b[main_`x'y:v_obs]/_b[main_`x'y:v_obs_pers] - 1)*100''% higher than fixed observable heterogeneity."
di "Total observable heterogeneity (control sample) is `: di %5.2f `= (_b[main_`x'y:v_obs_control]/_b[main_`x'y:v_obs_pers] - 1)*100''% higher than fixed observable heterogeneity."
di "Lower bound on total heterogeneity is `: di %8.5f `=_b[main_`x'y:v_pers] + _b[main_`x'y:v_obs] - _b[main_`x'y:v_obs_pers]''"

* Export table to csv:
cap frame drop tabl
frame create tabl str30(obs) double(mult_pers mult_trans mult_total ref_total)
frame post tabl ("Observable") (_b[main_`x'y:v_obs_pers]) (_b[main_`x'y:v_obs_trans]) (_b[main_`x'y:v_obs]) (_b[main_`x'y:v_obs_control])
frame post tabl ("") (_se[main_`x'y:v_obs_pers]) (_se[main_`x'y:v_obs_trans]) (_se[main_`x'y:v_obs]) (_se[main_`x'y:v_obs_control])
frame post tabl ("Unobservable") (_b[main_`x'y:v_unobs_pers]) (.) (.) (.)
frame post tabl ("") (_se[main_`x'y:v_unobs_pers]) (.) (.) (.)
frame post tabl ("Total") (_b[main_`x'y:v_pers]) (.) (_b[main_`x'y:v]) (_b[main_`x'y:v_control])
frame post tabl ("") (_se[main_`x'y:v_pers]) (.) (_se[main_`x'y:v]) (_se[main_`x'y:v_control])
frame tabl: export delimited using "${output}/${id_code}_TwoSpells_Table_Main.csv", replace

* Write LaTeX table:
file open myfile using "${output}/${id_code}_TwoSpells_Table_Main.tex", write replace

file write myfile "\documentclass{article}" _newline ///
	"\usepackage{multirow}" _newline ///
	"\usepackage{booktabs}" _newline ///	
	"\usepackage[margin=1in]{geometry}" _newline ///
	"\begin{document}" _newline ///
	"\begin{table}[h] \centering" _newline ///
	"\begin{tabular}{@{\extracolsep{8pt}} l c c c c}" _newline ///
	"\hline \hline \addlinespace[0.2cm]" _newline ///
	"& \multicolumn{3}{c}{Multiple Spells Sample} & Reference Sample \\  \addlinespace[0.1cm]" _newline ///
	"\cline{2-4} \cline{5-5} \addlinespace[0.1cm]" _newline ///
	"\hspace{2cm} & \textbf{Persistent} & \textbf{Transitory} & \textbf{Total} & \textbf{Total} \\ \addlinespace[0.2cm]" _newline ///
	"\multirow{2}{*}{\textbf{Observable}} & \(POH = Cov_{0,0}^{t_1,t_2}(\hat{F}_{0}^{t_1}, F_{0}^{t_2})\) & \(OH - POH \)  & \( OH = Cov_{0,0}^{t_1,t_2}(\hat{F}_{0}^{t_2}, F_{0}^{t_2})\) & \( \tilde{OH} = Cov_{0}^{t_2}(\hat{F}_{0}^{t_2}, F_{0}^{t_2})\) \\"  _newline ///
	" & \textbf{`: di %5.3f _b[main_`x'y:v_obs_pers]'} & \textbf{`: di %5.3f _b[main_`x'y:v_obs_trans]'} & \textbf{`: di %5.3f _b[main_`x'y:v_obs]'} & \textbf{`: di %5.3f _b[main_`x'y:v_obs_control]'} \\" _newline ///
	" & (`: di %5.4f _se[main_`x'y:v_obs_pers]') & (`: di %5.4f _se[main_`x'y:v_obs_trans]') & (`: di %5.4f _se[main_`x'y:v_obs]') & (`: di %5.4f _se[main_`x'y:v_obs_control]') \\ \addlinespace[0.2cm]" _newline ///
	"\multirow{2}{*}{\textbf{Unobservable}} & \( PH - POH \) & & &  \\" _newline ///
	" & \textbf{`: di %5.3f _b[main_`x'y:v_unobs_pers]'} & & & \\" _newline ///
	" & (`: di %5.4f _se[main_`x'y:v_unobs_pers]') & & & \\ \addlinespace[0.2cm]" _newline ///
	"\cline{2-4} \cline{5-5} \addlinespace[0.2cm]" _newline ///
	"\multirow{2}{*}{\textbf{Total}} & \(PH = Cov_{0,0}^{t_1,t_2}(F_{0}^{t_1}, F_{0}^{t_2}) \) & & \textit{By Proportionality} & \textit{By Proportionality} \\" _newline ///
	"& \textbf{`: di %5.3f _b[main_`x'y:v_pers]'} & & \textit{`: di %5.3f _b[main_`x'y:v]'} & \textit{`: di %5.3f _b[main_`x'y:v_control]'} \\" _newline ///
	"& (`: di %5.4f _se[main_`x'y:v_pers]') & & \textit{(`: di %5.4f _se[main_`x'y:v]')} & \textit{(`: di %5.4f _se[main_`x'y:v_control]')} \\  \addlinespace[0.2cm]" _newline ///
	"\hline \hline" _newline ///
	"\end{tabular}" _newline ///
	"\end{table}" _newline ///
	"\end{document}"

file close myfile	

* Write appendix LaTeX table:
file open myfile using "${output}/${id_code}_TwoSpells_Table_Appendix.tex", write replace

file write myfile "\documentclass{article}" _newline ///
	"\usepackage{multirow}" _newline ///
	"\usepackage{booktabs}" _newline ///	
	"\usepackage[margin=1in]{geometry}" _newline ///
	"\begin{document}" _newline ///
	"\begin{table}[h] \centering" _newline ///
	"\begin{tabular}{@{\extracolsep{8pt}} l c c c c}" _newline ///
	"\hline \hline \addlinespace[0.2cm]" _newline ///

foreach x in 0 2 5 {

	if `x' == 0 {
		local title "A. Baseline (Spells in different calendar years)"
	}
	
	if `x' == 2 {
		local title "B. Spells more than 2 years apart"
	}	
	
	if `x' == 5 {
		local title "C. Spells more than 5 years apart"
	}

	
	* Write to table:
	file write myfile ///
		"& \multicolumn{4}{c}{\large \textbf{`title'}} \\ \addlinespace[1.5ex]" _newline ///
		"& \multicolumn{3}{c}{Multiple Spells Sample} & Reference Sample \\  \addlinespace[0.1cm]" _newline ///
		"\cline{2-4} \cline{5-5} \addlinespace[0.1cm]" _newline ///
		"\hspace{2cm} & \textbf{Persistent} & \textbf{Transitory} & \textbf{Total} & \textbf{Total} \\ \addlinespace[0.2cm]" _newline ///
		"\multirow{2}{*}{\textbf{Observable}} & \(POH = Cov_{0,0}^{t_1,t_2}(\hat{F}_{0}^{t_1}, F_{0}^{t_2})\) & \(OH - POH \)  & \( OH = Cov_{0,0}^{t_1,t_2}(\hat{F}_{0}^{t_2}, F_{0}^{t_2})\) & \( \tilde{OH} = Cov_{0}^{t_2}(\hat{F}_{0}^{t_2}, F_{0}^{t_2})\) \\"  _newline ///
		" & \textbf{`: di %5.3f _b[main_`x'y:v_obs_pers]'} & \textbf{`: di %5.3f _b[main_`x'y:v_obs_trans]'} & \textbf{`: di %5.3f _b[main_`x'y:v_obs]'} & \textbf{`: di %5.3f _b[main_`x'y:v_obs_control]'} \\" _newline ///
		" & (`: di %5.4f _se[main_`x'y:v_obs_pers]') & (`: di %5.4f _se[main_`x'y:v_obs_trans]') & (`: di %5.4f _se[main_`x'y:v_obs]') & (`: di %5.4f _se[main_`x'y:v_obs_control]') \\ \addlinespace[0.2cm]" _newline ///
		"\multirow{2}{*}{\textbf{Unobservable}} & \( PH - POH \) & & &  \\" _newline ///
		" & \textbf{`: di %5.3f _b[main_`x'y:v_unobs_pers]'} & & & \\" _newline ///
		" & (`: di %5.4f _se[main_`x'y:v_unobs_pers]') & & & \\ \addlinespace[0.2cm]" _newline ///
		"\cline{2-4} \cline{5-5} \addlinespace[0.2cm]" _newline ///
		"\multirow{2}{*}{\textbf{Total}} & \(PH = Cov_{0,0}^{t_1,t_2}(F_{0}^{t_1}, F_{0}^{t_2}) \) & & \textit{By Proportionality} & \textit{By Proportionality} \\" _newline ///
		"& \textbf{`: di %5.3f _b[main_`x'y:v_pers]'} & & \textit{`: di %5.3f _b[main_`x'y:v]'} & \textit{`: di %5.3f _b[main_`x'y:v_control]'} \\ " _newline ///
		"& (`: di %5.4f _se[main_`x'y:v_pers]') & & \textit{(`: di %5.4f _se[main_`x'y:v]')} & \textit{(`: di %5.4f _se[main_`x'y:v_control]')} \\  \addlinespace[0.2cm]" _newline ///
		"\addlinespace[6ex]" _newline ///

}	

file write myfile ///
	"\addlinespace[-5ex]" _newline ///
	"\hline \hline" _newline ///
	"\end{tabular}" _newline ///
	"\end{table}" _newline ///
	"\end{document}"

file close myfile